home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / binaries / Windows / BDK / apis / java / beans / PropertyChangeSupport.java < prev    next >
Encoding:
Java Source  |  1997-03-17  |  4.0 KB  |  148 lines

  1. /*
  2.  * @(#)PropertyChangeSupport.java    1.11 97/02/03  
  3.  * 
  4.  * Copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion bdk_beta
  20.  * 
  21.  */
  22.  
  23. package java.beans;
  24.  
  25. import java.io.Serializable;
  26. import java.io.ObjectOutputStream;
  27. import java.io.ObjectInputStream;
  28. import java.io.IOException;
  29.  
  30.  
  31. /**
  32.  * This is a utility class that can be used by beans that support bound
  33.  * properties.  You can either inherit from this class or you can use 
  34.  * an instance of this class as a member field of your bean and delegate
  35.  * various work to it.
  36.  */
  37.  
  38. public class PropertyChangeSupport implements java.io.Serializable {
  39.  
  40.     /**
  41.      * @sourceBean  The bean to be given as the source for any events.
  42.      */
  43.  
  44.     public PropertyChangeSupport(Object sourceBean) {
  45.     source = sourceBean;
  46.     }
  47.  
  48.     /**
  49.      * Add a PropertyChangeListener to the listener list.
  50.      *
  51.      * @param listener  The PropertyChangeListener to be added
  52.      */
  53.  
  54.     public synchronized void addPropertyChangeListener(
  55.                 PropertyChangeListener listener) {
  56.     if (listeners == null) {
  57.         listeners = new java.util.Vector();
  58.     }
  59.     listeners.addElement(listener);
  60.     }
  61.  
  62.     /**
  63.      * Remove a PropertyChangeListener from the listener list.
  64.      *
  65.      * @param listener  The PropertyChangeListener to be removed
  66.      */
  67.  
  68.     public synchronized void removePropertyChangeListener(
  69.                 PropertyChangeListener listener) {
  70.     if (listeners == null) {
  71.         return;
  72.     }
  73.     listeners.removeElement(listener);
  74.     }
  75.  
  76.     /**
  77.      * Report a bound property update to any registered listeners.
  78.      * No event is fired if old and new are equal and non-null.
  79.      *
  80.      * @param propertyName  The programmatic name of the property
  81.      *        that was changed.
  82.      * @param oldValue  The old value of the property.
  83.      * @param newValue  The new value of the property.
  84.      */
  85.     public void firePropertyChange(String propertyName, 
  86.                     Object oldValue, Object newValue) {
  87.  
  88.     if (oldValue != null && oldValue.equals(newValue)) {
  89.         return;
  90.     }
  91.  
  92.     java.util.Vector targets;
  93.     synchronized (this) {
  94.         if (listeners == null) {
  95.             return;
  96.         }
  97.         targets = (java.util.Vector) listeners.clone();
  98.     }
  99.         PropertyChangeEvent evt = new PropertyChangeEvent(source,
  100.                         propertyName, oldValue, newValue);
  101.  
  102.     for (int i = 0; i < targets.size(); i++) {
  103.         PropertyChangeListener target = (PropertyChangeListener)targets.elementAt(i);
  104.         target.propertyChange(evt);
  105.     }
  106.     }
  107.  
  108.  
  109.     private void writeObject(ObjectOutputStream s) throws IOException {
  110.         s.defaultWriteObject();
  111.  
  112.     java.util.Vector v = null;
  113.     synchronized (this) {
  114.         if (listeners != null) {
  115.             v = (java.util.Vector) listeners.clone();
  116.             }
  117.     }
  118.  
  119.     if (v != null) {
  120.         for(int i = 0; i < v.size(); i++) {
  121.             PropertyChangeListener l = (PropertyChangeListener)v.elementAt(i);
  122.             if (l instanceof Serializable) {
  123.                 s.writeObject(l);
  124.             }
  125.             }
  126.         }
  127.         s.writeObject(null);
  128.     }
  129.  
  130.  
  131.     private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
  132.         s.defaultReadObject();
  133.       
  134.         Object listenerOrNull;
  135.         while(null != (listenerOrNull = s.readObject())) {
  136.       addPropertyChangeListener((PropertyChangeListener)listenerOrNull);
  137.         }
  138.     }
  139.  
  140.     transient private java.util.Vector listeners;
  141.     private Object source;
  142.     private int propertyChangeSupportSerializedDataVersion = 1;
  143. }
  144.  
  145.  
  146.  
  147.  
  148.